package com.aizuda.boot.modules.business.permission.service.impl;

import com.aizuda.boot.modules.business.permission.domain.entity.MenuRoleRelEntity;
import com.aizuda.boot.modules.common.constant.enums.PermissionCommonConstants;
import com.aizuda.boot.modules.business.permission.mapper.MenuRoleRelMapper;
import com.aizuda.boot.modules.business.permission.service.MenuRoleRelService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;


@Slf4j
@Service
public class MenuRoleRelServiceImpl extends ServiceImpl<MenuRoleRelMapper, MenuRoleRelEntity> implements MenuRoleRelService {


    @Override
    public List<Long> getIdsByRoleId(Long roleId) {
        QueryWrapper<MenuRoleRelEntity> queryWrapper = new QueryWrapper();

        queryWrapper.select("id").eq("role_id",roleId);
        queryWrapper.eq("is_deleted", PermissionCommonConstants.NOT_DELETED);

        List<MenuRoleRelEntity> list = baseMapper.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(list)){
            return new ArrayList<Long>();
        }
        return list.stream().map(MenuRoleRelEntity::getId).collect(Collectors.toList());
    }

    /**
     * 根据角色id或菜单id删除菜单角色关联表数据，不delete
     *
     * @param roleIds 角色id或菜单id
     * @param classification 分类PC/H5/小程序等
     * @param type 0：角色id 1：菜单id
     */
    @Override
    public Boolean deleteByRoleIds(List<Long> roleIds, String classification,Integer type) {
        int count;
        // MENU_ROLE_REL_TYPE_ROLE 0：角色id 1
        if(type == null || PermissionCommonConstants.MENU_ROLE_REL_TYPE_ROLE.equals(type)) {
            count = baseMapper.deleteByRoleIds(roleIds,null,classification);
        } else {
            count = baseMapper.deleteByRoleIds(null,roleIds,classification);
        }
        return count > -1?true:false;
    }

}
